package librarianPanels;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Rectangle;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTabbedPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.ListSelectionModel;
import javax.swing.border.LineBorder;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import javax.swing.table.DefaultTableModel;

import clientGUI.PanelDesign;
import clientGUI.MainGUI;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.ImageIcon;

/**
 * @author Shay
 *Watch Reader Card Panel
 */
public class WatchReaderCardPanel extends PanelDesign {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	public JTextField textFieldSubsciber;
	private static JTable table;
	private JTextField textFieldDateReturn;
	private static JTextField textFieldName;
	private static JTextField textFieldLastName;
	private static JTextField textFieldPhone;
	private static JTextField textFieldEmail;
	private static JLabel lblSubscriberId;
	public static JTextField textFieldStatus;
	public static JButton btnSearch;
	public String lastId;//for Refresh
	private JTextField txtOldDate;
	private static JTable actionsTable;
	private JLabel y;
	private JPanel watchReader;
	public JTabbedPane tabbedPane;
	/**
	 * Create the panel.
	 */
	public WatchReaderCardPanel() 
	{
		background.setSize(638, 456);
		setLayout(null);
		setPreferredSize(new Dimension(644, 462));
		watchReader=new JPanel();
		watchReader.setLayout(null);
		tabbedPane = new JTabbedPane(JTabbedPane.TOP);
		tabbedPane.setBounds(3, 3, 638, 456);
		tabbedPane.addTab("Subscriber Details",watchReader);
		
	background.setLocation(3, 3);
	setBorder(new LineBorder(new Color(0, 0, 0), 3, true));
	setBounds(0, 85, 644, 462);
	setLayout(null);
	lastId=new String();
	
	JPanel panel_1 = new JPanel();
	panel_1.setBounds(140, 39, 352, 44);
	watchReader.add(panel_1);
	panel_1.setLayout(null);
	textFieldSubsciber = new JTextField();
	textFieldSubsciber.setBounds(100, 11, 138, 22);
	panel_1.add(textFieldSubsciber);
	textFieldSubsciber.setColumns(10);
	
	btnSearch = new JButton("Search");
	btnSearch.setBounds(266, 0, 86, 44);
	panel_1.add(btnSearch);
	
	JLabel lblSubscriberId_1 = new JLabel("Subscriber ID:");
	lblSubscriberId_1.setBounds(0, 14, 100, 14);
	panel_1.add(lblSubscriberId_1);
	lblSubscriberId_1.setFont(new Font("Tahoma", Font.PLAIN, 14));
	btnSearch.addActionListener(new ActionListener() {
		public void actionPerformed(ActionEvent arg0) 
		{
			String[] msg=new String[2];
			msg[0]="WatchReaderCard";
			msg[1]=textFieldSubsciber.getText();		
			if(MainGUI.isMsgEmpty(msg))
			{
				JOptionPane.showMessageDialog(null,"the field is empty!");
				return;
			}
			if(!MainGUI.isNumber(msg[1]))
			{
				JOptionPane.showMessageDialog(null,"Only Digits");
				lastId=new String();
				return;
			}
			lastId=new String(msg[1]);
			MainGUI.OBLClient.handleMessageFromClient(msg);
		}


	});
	
	table = new JTable();
	table.addMouseListener(new MouseAdapter() {
		@Override
		public void mouseClicked(MouseEvent e) 
		{
			txtOldDate.setText((String) table.getValueAt(table.getSelectedRow(), 3));
			DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd");
			Calendar returnDate = Calendar.getInstance();
			try 
			{
				returnDate.setTime(dateFormat.parse(((String) table.getValueAt(table.getSelectedRow(), 3))));
			} 
			catch (ParseException e1) 
			{
				e1.printStackTrace();
			}
			if (bookIsPopular((String)table.getValueAt(table.getSelectedRow(), 8))) 
			{
				returnDate.add(Calendar.DATE, 3);
			} 
			else
				returnDate.add(Calendar.DATE, 14);
			String returnD =dateFormat.format(returnDate.getTime());
			textFieldDateReturn.setText(returnD);
		}

		private boolean bookIsPopular(String valueAt) 
		{
			if(valueAt.compareTo("1")==0)
				return true;
			return false;
		}
	});
	table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
	table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);

	table.setModel(new DefaultTableModel(
		new Object[][] {
		},
		new String[] {
			"Book ID", "copy", "Borrow Date", "Expected Date", "Incident", "Return Date", "Name", "Author", "Is Popular"
		}
	));
	table.getColumnModel().getColumn(0).setResizable(false);
	table.getColumnModel().getColumn(0).setPreferredWidth(59);
	table.getColumnModel().getColumn(1).setResizable(false);
	table.getColumnModel().getColumn(1).setPreferredWidth(39);
	table.getColumnModel().getColumn(2).setResizable(false);
	table.getColumnModel().getColumn(2).setPreferredWidth(87);
	table.getColumnModel().getColumn(3).setResizable(false);
	table.getColumnModel().getColumn(3).setPreferredWidth(98);
	table.getColumnModel().getColumn(4).setResizable(false);
	table.getColumnModel().getColumn(4).setPreferredWidth(67);
	table.getColumnModel().getColumn(5).setResizable(false);
	table.getColumnModel().getColumn(5).setPreferredWidth(89);
	table.getColumnModel().getColumn(6).setResizable(false);
	table.getColumnModel().getColumn(6).setPreferredWidth(100);
	table.getColumnModel().getColumn(7).setResizable(false);
	table.getColumnModel().getColumn(7).setPreferredWidth(85);
	table.setBounds(50, 133, 534, 231);
	//table.setAutoResizeMode(JTable.AUTO_RESIZE_NEXT_COLUMN);
	table.getTableHeader().setReorderingAllowed(false); 
	JScrollPane test = new JScrollPane(table);
	test.setVisible(true);
	test.setBounds(30, 184, 586, 152);
	watchReader.add(test);
	
	JLabel lblUpDateReturn = new JLabel("Update return date:");
	lblUpDateReturn.setBackground(new Color(245, 245, 245));
	lblUpDateReturn.setFont(new Font("Tahoma", Font.BOLD, 16));
	lblUpDateReturn.setBounds(232, 359, 189, 25);
	watchReader.add(lblUpDateReturn);
	
	JButton btnSumDate = new JButton("Submit");
	btnSumDate.addActionListener(new ActionListener() {
		public void actionPerformed(ActionEvent e) 
		{
			int n = JOptionPane.showConfirmDialog(new JFrame(),	"Are you really that sure?","An Inane Question",JOptionPane.YES_NO_OPTION);		
			if(n==1)		
				return;		
			String[] msg=new String[6];
			msg[0]="UpdateReturnDate";
			msg[1]=textFieldSubsciber.getText();//sid
			try
			{
				if(((String) table.getValueAt(table.getSelectedRow(), 5)).compareTo("00/00/00")!=0)
				{
					JOptionPane.showMessageDialog(null,"This book was returned");
					return;
				}
				msg[2]=(String) table.getValueAt(table.getSelectedRow(), 0);//bid
				msg[3]=(String) table.getValueAt(table.getSelectedRow(), 1);//copy
			}
			catch(ArrayIndexOutOfBoundsException e1)
			{
				JOptionPane.showMessageDialog(null,"The fields are empty!");
				return;
			}
			msg[4]=textFieldDateReturn.getText();//newreturn Date**need to check if date is correct input
			msg[5]=txtOldDate.getText();//oldreturn Date**need to check if date is correct input
			if(MainGUI.isMsgEmpty(msg))
			{
				JOptionPane.showMessageDialog(null,"The fields are empty!");
				return;
			}
			MainGUI.OBLClient.handleMessageFromClient(msg);
		}
	});
	btnSumDate.setBounds(454, 389, 100, 32);
	watchReader.add(btnSumDate);
	
	textFieldDateReturn = new JTextField();
	textFieldDateReturn.setBounds(351, 395, 86, 20);
	watchReader.add(textFieldDateReturn);
	textFieldDateReturn.setColumns(10);
	textFieldDateReturn.setEditable(false);
	
	JPanel panel = new JPanel();
	panel.setBackground(new Color(0,0,0,0));
	panel.setBounds(210, 78, 415, 70);
	watchReader.add(panel);
	panel.setLayout(null);
	
	JLabel lblNewLabel = new JLabel("First Name:");
	lblNewLabel.setBounds(0, 0, 63, 38);
	panel.add(lblNewLabel);
	lblNewLabel.setFont(new Font("Tahoma", Font.PLAIN, 12));
	
	textFieldName = new JTextField();
	textFieldName.setBounds(71, 9, 120, 20);
	panel.add(textFieldName);
	textFieldName.setEditable(false);
	textFieldName.setColumns(10);
	
	JLabel lblLastName = new JLabel("Last Name:");
	lblLastName.setBounds(0, 49, 61, 14);
	panel.add(lblLastName);
	lblLastName.setFont(new Font("Tahoma", Font.PLAIN, 12));
	
	JLabel lblNewLabel_1 = new JLabel("Phone Number:");
	lblNewLabel_1.setBounds(200, 8, 100, 22);
	panel.add(lblNewLabel_1);
	lblNewLabel_1.setFont(new Font("Tahoma", Font.PLAIN, 12));
	
	textFieldLastName = new JTextField();
	textFieldLastName.setBounds(71, 46, 120, 20);
	panel.add(textFieldLastName);
	textFieldLastName.setEditable(false);
	textFieldLastName.setColumns(10);
	
	textFieldPhone = new JTextField();
	textFieldPhone.setBounds(290, 10, 120, 20);
	panel.add(textFieldPhone);
	textFieldPhone.setEditable(false);
	textFieldPhone.setColumns(10);
	
	JLabel lblNewLabel_2 = new JLabel("E-Mail Address:");
	lblNewLabel_2.setBounds(200, 49, 84, 14);
	panel.add(lblNewLabel_2);
	lblNewLabel_2.setFont(new Font("Tahoma", Font.PLAIN, 12));
	
	textFieldEmail = new JTextField();
	textFieldEmail.setBounds(290, 47, 120, 20);
	panel.add(textFieldEmail);
	textFieldEmail.setEditable(false);
	textFieldEmail.setColumns(10);
	
	JLabel lblNewLabel_3 = new JLabel("Card Status:");
	lblNewLabel_3.setFont(new Font("Tahoma", Font.PLAIN, 12));
	lblNewLabel_3.setBounds(59, 89, 72, 14);
	watchReader.add(lblNewLabel_3);
	
	textFieldStatus = new JTextField();
	textFieldStatus.setEditable(false);
	textFieldStatus.setBounds(135, 87, 63, 20);
	watchReader.add(textFieldStatus);
	textFieldStatus.setColumns(10);
	
	JButton btnNewRefresh = new JButton("");
	btnNewRefresh.setIcon(new ImageIcon("images/refresh.jpg"));
	btnNewRefresh.addActionListener(new ActionListener() {
		public void actionPerformed(ActionEvent arg0) 
		{
			String[] msg=new String[2];
			msg[0]="WatchReaderCard";
			msg[1]=lastId;			
			if(MainGUI.isMsgEmpty(msg))
			{
				JOptionPane.showMessageDialog(null,"the field is empty!");
				return;
			}
			if(!MainGUI.isNumber(msg[1]))
			{
				JOptionPane.showMessageDialog(null,"Only Digits");
				lastId=new String();
				return;
			}
			MainGUI.OBLClient.handleMessageFromClient(msg);
		}
	});
	btnNewRefresh.setBounds(300, 150, 33, 30);
	watchReader.add(btnNewRefresh);
	
	JLabel lblOld = new JLabel("Old:");
	lblOld.setBounds(152, 398, 40, 16);
	watchReader.add(lblOld);
	
	txtOldDate = new JTextField();
	txtOldDate.setEditable(false);
	txtOldDate.setBounds(188, 395, 116, 22);
	watchReader.add(txtOldDate);
	txtOldDate.setColumns(10);
	

	
	JPanel actions=new JPanel();
	tabbedPane.addTab("Subscriber Actions", actions);
	actions.setLayout(null);
	
	JLabel lblMyActions = new JLabel("Subscriber Actions:");
	lblMyActions.setFont(new Font("Tahoma", Font.BOLD, 16));
	lblMyActions.setBounds(257, 30, 183, 28);
	actions.add(lblMyActions);
	
	JScrollPane scrollPane_2 = new JScrollPane();
	scrollPane_2.setBounds(99, 68, 435, 217);
	actions.add(scrollPane_2);
	
	actionsTable = new JTable();
	scrollPane_2.setViewportView(actionsTable);
	
	
	actionsTable.setModel(new DefaultTableModel(
			new Object[][] {
				{null, null}, {null, null},
				{null, null}, {null, null},
				{null, null}, {null, null},
				{null, null}, {null, null},
				{null, null}, {null, null},
				{null, null}, {null, null},
				{null, null}, {null, null},
				{null, null}, {null, null},
		
			},
			new String[] {
				"Date", "Description"
			}));

	
	add(tabbedPane);
	
	
	
	JLabel lblNewLabel_4 = new JLabel("New:");
	lblNewLabel_4.setBounds(310, 398, 40, 16);
	watchReader.add(lblNewLabel_4);
	
	JLabel lblWatchReaderCard = new JLabel("Watch Reader Card");
	lblWatchReaderCard.setFont(new Font("Tahoma", Font.PLAIN, 29));
	lblWatchReaderCard.setBounds(175, -5, 330, 49);
	watchReader.add(lblWatchReaderCard);
	remove(background);
	add(background);
	JLabel x=new JLabel();
	x.setIcon(background.getIcon());
	x.setBounds(new Rectangle(0, 0, 638, 456));
	y=new JLabel();
	y.setIcon(new ImageIcon("images/test35.jpg"));
	y.setBounds(new Rectangle(0, 0, 638, 456));
	actions.add(x);

}

	/**
	 * Show result from server
	 * @param actions - table with the actions of this subscriber
	 * @param infoTable - table resault from the server
	 * @param infoSubscriber - info about subscriber from server
	 * @param isBidOk - true if bid ok else false not ok
	 */
	public static void changeTableContent(String[][] actions,String[][] infoTable,String[][] infoSubscriber, boolean isBidOk)
	{
		if(isBidOk)
		{
			textFieldName.setText(infoSubscriber[0][0]);
			textFieldLastName.setText(infoSubscriber[0][1]);
			textFieldPhone.setText(infoSubscriber[0][2]);
			textFieldEmail.setText(infoSubscriber[0][3]);
			if(infoSubscriber[0][4].compareTo("1")==0)
			{
				textFieldStatus.setText("Active");
			}
			else if(infoSubscriber[0][4].compareTo("0")==0)
			{
				textFieldStatus.setText("Locked");
			}
			else
				textFieldStatus.setText("Frozen");
		}
		else
		{
			textFieldName.setText("N/A");
			textFieldLastName.setText("N/A");
			textFieldPhone.setText("N/A");
			textFieldEmail.setText("N/A");
			textFieldStatus.setText("N/A");
			table.setModel(new DefaultTableModel(null,		new String[] 
					{
							"Book ID", "copy", "Borrow Date", "Expected Date", "Incident", "Return Date", "Name", "Author", "Is Popular"
						}));
			return;
		}
		for(int i=0;i<infoTable.length;i++)
		{
			if(infoTable[i][8].compareTo("1")==0)
			{
				infoTable[i][8]="Yes";
			}
			else
			{
				infoTable[i][8]="NO";
			}
		}
		table.setModel(new DefaultTableModel(infoTable,		new String[] 
		{
				"Book ID", "copy", "Borrow Date", "Expected Date", "Incident", "Return Date", "Name", "Author", "Is Popular"
			}){
			private static final long serialVersionUID = 1L;
			public boolean isCellEditable(int row, int column) {
				return false;
			}
		});
		table.getColumnModel().getColumn(0).setResizable(false);
		table.getColumnModel().getColumn(0).setPreferredWidth(59);
		table.getColumnModel().getColumn(1).setResizable(false);
		table.getColumnModel().getColumn(1).setPreferredWidth(39);
		table.getColumnModel().getColumn(2).setResizable(false);
		table.getColumnModel().getColumn(2).setPreferredWidth(87);
		table.getColumnModel().getColumn(3).setResizable(false);
		table.getColumnModel().getColumn(3).setPreferredWidth(98);
		table.getColumnModel().getColumn(4).setResizable(false);
		table.getColumnModel().getColumn(4).setPreferredWidth(67);
		table.getColumnModel().getColumn(5).setResizable(false);
		table.getColumnModel().getColumn(5).setPreferredWidth(89);
		table.getColumnModel().getColumn(6).setResizable(false);
		table.getColumnModel().getColumn(6).setPreferredWidth(150);
		table.getColumnModel().getColumn(7).setResizable(false);
		table.getColumnModel().getColumn(7).setPreferredWidth(85);
		changeTableContentMyActions(actions);
		
	}
	
	
	
	/**
	 * Show result from server
	 * @param infoTable - table resault from the server
	 */
	public static void changeTableContentMyActions(String[][] infoTable){
		actionsTable.setModel(new DefaultTableModel(infoTable, new String[] {
				"Date" , "Description"
			}){
				private static final long serialVersionUID = 1L;
				public boolean isCellEditable(int row, int column) {
					return false;
				}
			  });
		actionsTable.getTableHeader().setReorderingAllowed(false); 
		actionsTable.getColumnModel().getColumn(0).setResizable(false);
		actionsTable.getColumnModel().getColumn(1).setResizable(false);
		actionsTable.getColumnModel().getColumn(0).setPreferredWidth(50);
		actionsTable.getColumnModel().getColumn(1).setPreferredWidth(250);
			}
	
	


	/**
	 * Show message about update return date
	 * @param msg
	 */
	public static void ShowMsgFromServer(String msg) 
	{
			if(msg.contains("FAIL"))
			{
				JOptionPane.showMessageDialog(null,"Operation failed");
				return;
			}
			JOptionPane.showMessageDialog(null,"The action succeeded!");
	}
}
